package com.yituo.mjworld.mvc.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.yituo.mjworld.core.web.core.BaseForm;
import com.yituo.mjworld.core.web.core.Result;
import com.yituo.mjworld.mvc.dao.ShopCatDao;
import com.yituo.mjworld.mvc.entity.web.WebShopCut;
import com.yituo.mjworld.mvc.form.DeleteForm;
import com.yituo.mjworld.mvc.form.QueryForm;
import com.yituo.mjworld.mvc.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Date;

/**
 * 购物车模块
 *
 * @author Administrator
 */
@Service
public class ShopCatServiceImpl extends BaseService {

  @Autowired
  ShopCatDao shopCatDao;

  /**
   * 获取全部的购物车清单
   *
   * @param form 查询表单
   * @return
   */
  @Override
  public JSONObject getList(QueryForm form) {
    String countSql = "select count(id) from web_shop_cut";
    String sql = "select id," +
        "userId," +
        "goodsId," +
        "num," +
        "goodsPrice," +
        "createDate " +
        "from web_shop_cut";
    return get(sql, countSql, form, null);
  }

  /**
   * 获取某个用户购物车清单
   *
   * @param form   表单对象
   * @param userId 欲查询的用户ID
   * @return
   */
  public JSONObject getListForUser(QueryForm form, String userId) {
    String countSql = "select count(id) from web_shop_cut where userId=:userId";
    String sql = "select " +
        " a.id," +
        " a.productId," +
        " a.num," +
        " a.totalPrice," +
        " a.finalPrice," +
        " a.template," +
        " a.createDate," +
        " b.name," +
        " b.mark," +
        " b.salePrice," +
        " b.shoppePrice," +
        " b.thumbImgUrl " +
        " from web_shop_cut a left join product b on a.productId=b.id where a.userId=:userId order by a.createDate ";
    form.getParam().clear();
    form.getParam().put("userId", userId);
    return get(sql, countSql, form, form.getParam());
  }

  /**
   * 删除一条记录,无差别删除
   *
   * @param form 删除表单
   * @return
   */
  @Override
  public Result delete(DeleteForm form) {
    Result result = new Result();
    WebShopCut shopCut = (WebShopCut) baseDao.get(WebShopCut.class, form.getId());
    if (shopCut == null) {
      result.setResult(false).setMsg("您欲删除的商品不存在!");
    } else {
      baseDao.delete(WebShopCut.class, form.getId());
      result.setResult(true);
    }
    return result;
  }

  /**
   * 删除某个用户的记录,用于权限控制.
   *
   * @param form   删除表单
   * @param userId 用户ID,一般是currentUser中的ID,以防止当前用户删除他人的商品
   * @return
   */
  public Result deleteForUser(DeleteForm form, String userId) {
    Result result = new Result();
    WebShopCut shopCut = (WebShopCut) baseDao.get(WebShopCut.class, form.getId());
    if (shopCut == null) {
      result.result = false;
      result.msg = "您欲删除的商品不存在!";
    } else {
      if (!userId.equals(shopCut.getUserId())) {
        result.result = false;
        result.msg = "你不能删除其他人的商品!";
      } else {
        baseDao.delete(WebShopCut.class, form.getId());
        result.result = true;
      }
    }
    return result;
  }

  /**
   * 修改商品.
   * 一般为超级管理员调用!
   *
   * @param baseForm
   * @return
   */
  @Override
  public Result saveOrUpdate(BaseForm baseForm) {
    return null;
  }

  /**
   * 修改购物车中某商品数量.
   *
   * @return
   */
  public Result updateNum(int recordId, int newNum) {
    Result result = new Result();
    WebShopCut record_2 = (WebShopCut) baseDao.get(WebShopCut.class, recordId);
    if (record_2 == null) {
      return result.setResult(false).setMsg("您欲更新的记录不存在!");
    } else {
      // 更新数量
      record_2.setNum(newNum);
      // 更新总价
      record_2.setTotalPrice(record_2.getFinalPrice().multiply(BigDecimal.valueOf(record_2.getNum())));
      // 更新
      this.baseDao.update(record_2);
      return result.setResult(true);
    }
  }

  /**
   * 修改商品.
   * <p>
   * 一般为当前用户调用
   *
   * @param baseForm
   * @return
   */
  public Result saveOrUpdate(BaseForm baseForm, String userId) {
    return null;
  }

  /**
   * 修改商品.
   * <p>
   * 一般为当前用户调用
   *
   * @param baseForm
   * @return
   */
  public Result add2ShopCat(BaseForm baseForm, String userId) {
    Result result = new Result();
    WebShopCut record = (WebShopCut) baseForm;
    WebShopCut record3 = shopCatDao.getRecordProductIdAndUserId(record.getProductId(), userId);
    if (record3 != null) {
      // 该产品已经存在于用户的购物车当中
      if (record3.getTemplate().equals(record.getTemplate())) {
        // 新添加的产品记录如果在描述上数据库已经存在的记录相等,则新加入购物车的产品记录，可以和数据库已经存在的合并!
        record3.setNum(record3.getNum() + record.getNum());
        record3.setTotalPrice(record3.getFinalPrice().multiply(BigDecimal.valueOf(record3.getNum())));
        this.baseDao.update(record3);
        return result.setResult(true);
      }
    }
    record.setCreateDate(new Date());
    // 其余的都当作新记录保存
    this.baseDao.save(record);
    result.setResult(true);
    return result;
  }

  /**
   * 获取订单数
   *
   * @return
   */
  public int getNum(String userId) {
    String sql = "select count(id) from web_shop_cut where userId=:userId";
    BigInteger result = (BigInteger) this.baseDao.getSession().createNativeQuery(sql).setParameter("userId", userId).uniqueResult();
    return result.intValue();
  }
}
